import urllib
import urllib2
import socket
import cookielib

def taobao():
    # logfile.write(str(datetime.datetime.now()) +' taobao/r/n')
    cj = cookielib.CookieJar()
   
    post_data = urllib.urlencode(
        {
         'CtrlVersion': '1,0,0,7',
         'TPL_password':'cl2222',
         'TPL_redirect_url':'',
         'TPL_username':'hivanchen',
         '_tb_token_':'5d7eba30e3b31',
         'action':'Authenticator',
         'callback':'',
         'css_style':'',
         'event_submit_do_login':'anything',
         'fc':2,
         'from':'tb',
         'from_encoding':'',
         'guf':'',
         'gvfdcname':'',
         'isIgnore':'',
         'llnick':'',
         'loginType':3,
         'longLogin':0,
         'minipara' :'',
         'minititle':'',
         'need_sign':'',
         'need_user_id':'',
         'not_duplite_str':'',
         'popid':'',
         'poy':'',
         'pstrong':'',
         'sign':'',
         'style':'default',
         'support':'000001',
         'tid':''
        }
        )
    path = 'https://login.taobao.com/member/login.jhtml'
    opener = urllib2.build_opener(urllib2.HTTPCookieProcessor(cj))
 
    opener.addheaders = [('User-agent', 'Mozilla/5.0 (Windows; U; Windows NT 6.1; zh-CN; rv:1.9.2.13) Gecko/20101203 Firefox/3.6.13')]
   
    urllib2.install_opener(opener)
    req = urllib2.Request(path,post_data)
    req.addheader = ('Host', 'login.taobao.com')
    req.addheader = ('Referer', 'https://login.taobao.com/member/login.jhtml')
    #req.addheader = ('Content-Length', 414)
    req.addheader = ('Content-Type', 'application/x-www-form-urlencoded')
    #try login
    try:
        conn = urllib2.urlopen(req)
    except URLError,e:
        logfile.write('URLError:' +e.code + '/r/n')
        return False
    except HTTPError,e:   
        logfile.write('HTTP Error:'+e.reason + '/r/n')
        return False
    html = conn.read()
    with open('1.html','wu') as f:
        f.write(html)
    # #open tao jianhu,get currently coin
    # try:
    #     conn = urllib2.urlopen('http://qz.jianghu.taobao.com/home/award_bid_home.htm?tracelog=jhleftmenu')
    # except URLError,e:
    #     logfile.write('URLError:' +e.code + '/r/n')
    #     return False
    # except HTTPError,e:   
    #     logfile.write('HTTP Error:'+e.reason + '/r/n')
    #     return False
    # html = conn.read()
    # #seach my coin
    # digs = re.compile('<strong id="J_Coin">/d*')
    # result = digs.findall(html)
    # result = result[0].split('>')
    # #my currently coin
    # original_coin = result[1]
    # #get get_coin_path
    # digs = re.compile('"J_EnterTime" type="hidden" value=".*"')
    # result = digs.findall(html)
    # result = result[0].split('"')
    # enter_time = result[5]
    # #print enter_time
  
    # digs = re.compile('_tb_token_.*value=.*/'')
    # result = digs.findall(html)
   
    # result = result[0].split('/'')
    # tb_token = result[4]
  
    # get_coin_path = 'http://qz.jianghu.taobao.com/home/grant_everyday_coin.htm?t=1295486909134&_tb_token=5615ee3a50637'#+tb_token
    # #try get more coin
    # post_data = urllib.urlencode(
    #     {'enter_time':'1295486851783',#enter_time,
    #      'ran':'0.9395819079633437',
    #      'user_action':'tm=MTI5NTQ4Njg2MDM1OA==|ma=0rhVdv6+FAg=|br=jVF4vau4JmjnhXPejAYmduDU/IYjoBuD|pr=0rhVdv6+FAg=|id=0rhVdv6+FAg=|mc=BKZr5rSMYFoIFxHkfHzSFA==',
    #         })
    # req = urllib2.Request(path,post_data)
    # req.addheader = ('Host', 'qz.jianghu.taobao.com')
    # req.addheader = ('Referer', 'http://qz.jianghu.taobao.com/home/award_bid_home.htm?tracelog=jhleftmenu')
    # try:
    #     conn = urllib2.urlopen(req)
    # except URLError,e:
    #     logfile.write('URLError:' +e.code + '/r/n')
    #     return False
    # except HTTPError,e:   
    #     logfile.write('HTTP Error:'+e.reason + '/r/n')
    #     return False
  
    # #update currently coin
    # try:
    #     conn = urllib2.urlopen('http://qz.jianghu.taobao.com/home/award_bid_home.htm?tracelog=jhleftmenu')
    # except URLError,e:
    #     logfile.write('URLError:' +e.code + '/r/n')
    #     return False
    # except HTTPError,e:   
    #     logfile.write('HTTP Error:'+e.reason + '/r/n')
    #     return False
    # result = conn.read()
    # #seach my coin
    # digs = re.compile('<strong id="J_Coin">/d*')
    # result = digs.findall(result)
    # result = result[0].split('>')
    # #my currently coin
    # now_coin = result[1]
    # logfile.write('Your coin:'+original_coin+'->'+ now_coin+'/r/n')
#--------------------------------------------------------------------
if __name__ == '__main__' :
    taobao()
    # logfile = open('log.txt','a')
    # renren(logfile,'','')
    # csdn(logfile,'','')
    # taobao(logfile,'','')
    # logfile.close()
    print 'OK'

 